# api/keywords.py
"""
Author: didiplus
Description: 关键词相关API
Date: 2025-08-20 21:00:00
LastEditors: didiplus
LastEditTime: 2025-08-20 21:00:00
FilePath: /WeChatBot/app/api/keywords.py
Version: 1.0
"""
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
from typing import List

# 使用相对导入方式引入模块
from core.database import get_db
from schemas.keyword import (
    KeywordRule, KeywordRuleCreate, KeywordRuleUpdate, 
    TextInput, MatchResult, KeywordResponse
)
from services.keyword import KeywordRuleService
from api.deps import get_current_active_user

router = APIRouter()

@router.post("/", response_model=KeywordRule, status_code=status.HTTP_201_CREATED)
def create_keyword_rule(
    rule_create: KeywordRuleCreate,
    current_user: dict = Depends(get_current_active_user),
    db: Session = Depends(get_db)
):
    """
    创建关键词规则
    """
    keyword_service = KeywordRuleService(db)
    db_rule = keyword_service.create_keyword_rule(
        user_id=current_user.id, 
        rule_create=rule_create
    )
    return db_rule

@router.get("/", response_model=List[KeywordRule])
def read_keyword_rules(
    skip: int = 0,
    limit: int = 100,
    current_user: dict = Depends(get_current_active_user),
    db: Session = Depends(get_db)
):
    """
    获取当前用户的所有关键词规则
    """
    keyword_service = KeywordRuleService(db)
    rules = keyword_service.get_keyword_rules_by_user(
        user_id=current_user.id, 
        skip=skip, 
        limit=limit
    )
    return rules

@router.get("/{rule_id}", response_model=KeywordRule)
def read_keyword_rule(
    rule_id: int,
    current_user: dict = Depends(get_current_active_user),
    db: Session = Depends(get_db)
):
    """
    获取特定关键词规则
    """
    keyword_service = KeywordRuleService(db)
    db_rule = keyword_service.get_keyword_rule_by_id(rule_id=rule_id)
    
    if not db_rule:
        raise HTTPException(status_code=404, detail="关键词规则不存在")
    
    if db_rule.user_id != current_user.id:
        raise HTTPException(status_code=403, detail="无权限访问此规则")
    
    return db_rule

@router.put("/{rule_id}", response_model=KeywordRule)
def update_keyword_rule(
    rule_id: int,
    rule_update: KeywordRuleUpdate,
    current_user: dict = Depends(get_current_active_user),
    db: Session = Depends(get_db)
):
    """
    更新关键词规则
    """
    keyword_service = KeywordRuleService(db)
    db_rule = keyword_service.update_keyword_rule(
        rule_id=rule_id,
        user_id=current_user.id,
        rule_update=rule_update
    )
    
    if not db_rule:
        raise HTTPException(status_code=404, detail="关键词规则不存在")
    
    return db_rule

@router.delete("/{rule_id}")
def delete_keyword_rule(
    rule_id: int,
    current_user: dict = Depends(get_current_active_user),
    db: Session = Depends(get_db)
):
    """
    删除关键词规则
    """
    keyword_service = KeywordRuleService(db)
    success = keyword_service.delete_keyword_rule(
        rule_id=rule_id,
        user_id=current_user.id
    )
    
    if not success:
        raise HTTPException(status_code=404, detail="关键词规则不存在")
    
    return {"message": "关键词规则删除成功"}

@router.post("/match", response_model=MatchResult)
def match_keyword(
    input_data: TextInput,
    current_user: dict = Depends(get_current_active_user),
    db: Session = Depends(get_db)
):
    """
    匹配关键词并返回对应内容
    """
    keyword_service = KeywordRuleService(db)
    result = keyword_service.match_text_with_rules(
        text=input_data.text,
        user_id=current_user.id
    )
    return result

@router.post("/match/best", response_model=KeywordResponse)
def match_best_keyword(
    input_data: TextInput,
    current_user: dict = Depends(get_current_active_user),
    db: Session = Depends(get_db)
):
    """
    只返回最佳匹配结果
    """
    keyword_service = KeywordRuleService(db)
    result = keyword_service.match_text_with_rules(
        text=input_data.text,
        user_id=current_user.id
    )
    return result.best_match